print('#############可变参数，命名关键字参数，位置参数#############')

print('#############函数的参数，位置参数#############')


def power(x, n):
    s = 1
    while n > 0:
        s = s * x
        n = n - 1
    return s


print(power(2, 10))
print('#############函数的参数，默认参数，在形参中直接赋值#############')


def power(x, n=2):
    s = 1
    while n > 0:
        s = s * x
        n = n - 1
    return s


print(power(2))


def register(name, score, city='beijing', address='朝阳区'):
    print('name:', name)
    print('score:', score)
    print('city:', city)
    print('address:', address)


register('zhangsan', 120)
register('李四', 'A', address='玄武区')
print('#############函数的参数，默认参数必须指向不变对象！#############')


def invariant_default_param(list=None):
    if list is None:
        list = []
    list.append('END!')
    return list


# list=[]
print(invariant_default_param())
print(invariant_default_param())
print('#############函数的参数，默认参数必须指向不变对象！#############')


def invariant_default_param(list=['START!']):
    list.append('END!')
    return list


# list=[]
print(invariant_default_param())
print(invariant_default_param())
print('#############函数的参数，指向不变对象#############')


def add_end(list=None):
    if list is None:
        print(list is None)
        return []
    else:
        print(list is None)
        list.append("END!")
        return list


list2 = ['START!']
print(add_end(list2))
print(add_end())
print('#############函数的参数，参数可变，需要传入一个list，tuple#############')


def change_params(numbers):
    sum = 0
    for i in numbers:
        sum = sum + i * i
    print('sum = ', sum)
    return sum


change_params((1, 2, 3))
change_params([1, 2, 3])
print('#############函数的参数，参数可变，需要在参数前加上*#############')


def change_params2(*numbers):
    sum = 0
    for i in numbers:
        sum = sum + i * i
    print('sum = ', sum)
    return sum


change_params2(1, 2, 3, 4)
print('#############函数的参数，参数可变#############')
list = (1, 2, 3, 4)
change_params2(*list)
print(
    '#############函数的参数，关键字参数：关键字参数允许你传入0个或任意个含参数名的参数，这些关键字参数在函数内部自动组装为一个dict（键值对）。#############')


def person(name, sex, **kw):
    print('name = ', name, ' sex = ', sex, ' kw = ', kw)


person('张三', 1, height=174, weight=56)
person('张三', 1, 身高=174, 体重=56)
print(
    '#############函数的参数，关键字参数：关键字参数允许你传入0个或任意个含参数名的参数，这些关键字参数在函数内部自动组装为一个dict（键值对）。#############')
dict1 = {'height': 174, 'weight': 56}
person('lisi', 2, **dict1)
print(
    '#############函数的参数，关键字参数：关键字参数允许你传入0个或任意个含参数名的参数，这些关键字参数在函数内部自动组装为一个dict（键值对）。#############')
print('#############函数的参数，关键字参数：参数的检查#############')


def person(name, sex, **kw):
    if 'height' in kw:
        print('判断1')
        pass
    if 'weight' in kw:
        print('判断2')
        pass
    print('name = ', name, ' sex = ', sex, ' kw = ', kw)


person('lisi', 2, **dict1)
print('#############函数的参数，关键字参数：限制关键字参数的名字#############')


def person(name, sex, *, height, weight):
    print('name = ', name, ' sex = ', sex, ' kw = ', height, weight)


person('张三', 3, height='098', weight='98kg')
print(
    '#############函数的参数，关键字参数：函数定义中已经有了一个可变参数，后面跟着的命名关键字参数就不再需要一个特殊分隔符*了：#############')


def person(name, sex, *height, weight):
    print('name = ', name, ' sex = ', sex, ' kw = ', height, weight)


person('张三', 3, '987', weight='98kg')
